package designpattern.Observer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author cyt
 * @create 2020-08-13 13:41
 */
public class Store {

    ArrayList<ProductObserver> list = new ArrayList<ProductObserver>();
    private Map<String, Product> products = new HashMap<>();

    public void addProductObserver(ProductObserver productObserver) {
        list.add(productObserver);
    }

    public void removeProductObserver(ProductObserver productObserver) {
        list.remove(productObserver);
    }

    public void addProduct(Product product) {
        products.put(product.getName(), product);
        ProductEvent event = new ProductEvent(product,"添加");
        list.forEach(observer -> observer.onEvent(event));
    }

    public void setProductPrice(String name, String price) {
        Product product = products.get(name);
        ProductEvent event = new ProductEvent(product,"修改");
        product.setPrice(price);
        list.forEach(productObserver -> productObserver.onEvent(event));
    }
}
